/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.m003.ui.controllers;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;
import com.vaadin.data.util.BeanItemContainer;
import ep.libs.implementations.AbstractModule;
import ep.persistence.entities.RegisterPharmacistBean;
import ep.persistence.entities.SavePatientIllnessBean;
import ep.persistence.entities.T005Patient;
import ep.persistence.entities.T005T007;
import ep.persistence.entities.T007Illness;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

/**
 *
 * @author Spyros
 */
public class M003Controller {

    private T005Patient patient;
    private WebResource service;

    public M003Controller(Object patientObject, AbstractModule module) {
        this.patient = (T005Patient) patientObject;
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        config.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
        service = client.resource(UriBuilder.fromUri("http://localhost:8080/EPrescribing/rest").build());
    }

    public BeanItemContainer<T005T007> getTableContainer() {
        BeanItemContainer<T005T007> beanItemContainer;
        if (patient != null && patient.getT005T007Collection() != null && !patient.getT005T007Collection().isEmpty()) {
            beanItemContainer = new BeanItemContainer<T005T007>(T005T007.class, patient.getT005T007Collection());
        } else {
            beanItemContainer = new BeanItemContainer<T005T007>(T005T007.class);
        }
        return beanItemContainer;
    }

    public BeanItemContainer<T007Illness> getIllnessContainer() {
        Collection<T007Illness> illnesses = service.path("bl003").path("illnesses").
                get(new GenericType<Collection<T007Illness>>() {
        });
        return new BeanItemContainer<T007Illness>(T007Illness.class, illnesses);
    }

    public T005T007 getNewT005t007(Object illnessObject) {
        T005T007 t005t007 = new T005T007();
        t005t007.setT007Illness((T007Illness) illnessObject);
        return t005t007;
    }

    public boolean checkIfIllnessExists(Object illnessObject, Collection collection) {
        for (Iterator it = collection.iterator(); it.hasNext();) {
            T005T007 t005t007 = (T005T007) it.next();
            if (t005t007.getT007Illness().equals((T007Illness) illnessObject)) {
                return true;
            }
        }
        return false;
    }

    public boolean save(Collection t005t007Collection) {
        SavePatientIllnessBean bean = new SavePatientIllnessBean(patient, t005t007Collection);
        ClientResponse clientResponse = service.path("bl003").path("sillness").
                accept(MediaType.TEXT_PLAIN).post(
                ClientResponse.class, bean);
        String response = clientResponse.getEntity(String.class);
        return response.equals("0");
//        return bL003Controller.savePatientIllness(patient, t005t007Collection);
    }
}
